//订单管理
//施少伟奇
//fixed by zhaojian

/**
 * 订单管理列表视图
 */
interface IOrderManagentViewModel extends IBaseViewModel<IOrderManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<orderList>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 查询
     */
    query: () => void;
    /**
     * 订单编号
     */
    orderId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 路线地址
     */
    route: string;
    /**
     * 订单状态
     */
    status: string;
    /**
     * 调度专员
     */
    dispatchOfficerId: string;
    /**
     * 客服专员
     */
    customerServiceOfficerId: string;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliverAddress: string;
    /**
     * 发货起始时间
     */
    shipStartTime: string;
    /**
     * 发货结束时间
     */
    shipEndTime: string;
    /**
     * 下单起始时间
     */
    placeOrderStartTime: string;
    /**
     * 下单结束时间
     */
    placeOrderEndTime: string;
    /**
     * 订单状态下拉
     */
    statusList: ValueListItemResponse[];
    /**
     * 调度专员下拉
     */
    dispatchOfficerList: EmployeeListItemResponse[];
    /**
     * 填充客服专员下拉
     */
    customerOfficerList: EmployeeListItemResponse[];
    /**
     * 查看详情
     */
    orderAddEdit: () => void;
    /**
     * 导出报表
     */
    skipExport: () => void;
    /**
     * 列表高度
     */
    heightA:number;


}


/**
 * 查询条件
 */
interface IOrderManagentQueryTerms {
    /**
     * 订单编号
     */
    orderId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 路线地址
     */
    route: string;
    /**
     * 订单状态
     */
    status: string;
    /**
     * 发货起始时间
     */
    shipStartTime: string;
    /**
     * 发货结束时间
     */
    shipEndTime: string;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliverAddress: string;
    /**
     * 下单起始时间
     */
    placeOrderStartTime: string;
    /**
     * 下单结束时间
     */
    placeOrderEndTime: string;
    /**
     * 调度专员
     */
    dispatchOfficerId: string;
    /**
     * 客服专员
     */
    customerServiceOfficerId: string;

}

class OrderManagentController extends ListController<IOrderManagentViewModel, IOrderManagentQueryTerms>{

    gridApi: uiGrid.IGridApiOf<orderList>;
    /**
     * 排序字段枚举
     */
    orderBy: number = 0;
    /**
     * 是否升序
     */
    asc: boolean = true;
    /**
     * 订单编号id(标识符)
     */
    id: string;

    skip:number = 0;

    constructor($scope: IOrderManagentViewModel,
        private orderService: IorderService,
        private valueService: IValueService,
        private $ngBootbox: BootboxService,
        private $state: angular.ui.IStateService,
        private employeeService: IEmployeeService,
        private routeService:routeService,
        private $http: ng.IHttpService,
        private $location:ng.ILocationService,private exportService:IexportService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        this.$scope.orderAddEdit = this.orderAddEdit;
        this.id = "";


        this.$scope.queryTerms = {
            orderId: "",
            clientName: "",
            route: "",
            status: "1",
            shipStartTime: "",
            shipEndTime: "",
            shipAddress: "",
            deliverAddress: "",
            placeOrderStartTime: "",
            placeOrderEndTime: "",
            dispatchOfficerId: "",
            customerServiceOfficerId: ""
        }

        this.$scope.gridOptions = {
            enableFullRowSelection: true,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            exporterMenuCsv: false,
            useExternalSorting: false,
            enableSelectAll: false,//全选禁用
            enableColumnResizing: true,
            exporterMenuPdf: false,
            enableGridMenu: true,
            multiSelect: false,

            columnDefs: [{
                displayName: "订单编号", field: 'orderId', enableColumnMenu: false, width: '9%', cellTooltip: true, headerTooltip: true,
            }, {
                displayName: "客户单位", field: 'clientName', enableColumnMenu: false, width: '10%', cellTooltip: true, headerTooltip: true,
            }, {
                displayName: "发货地址", field: 'startAddress', enableColumnMenu: false, width: '9%', cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, width: '9%', cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '货物数量', field: 'goodsNum', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '发货时间', field: 'shipTime', enableColumnMenu: false, width: '9%', cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '调度专员', field: 'dispatcher', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '客服专员', field: 'customerService', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '紧急程度', field: 'urgency', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '订单状态', field: 'orderStatus', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }, {
                displayName: '下单时间', field: 'placeOrderTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true,
            }
            ],
            exporterFieldCallback: function (grid, row, col, value) {
                if (col.name == "orderId") {
                    return "\uFEFF" + value;
                }
                return value;
            },
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                    this.paginationChanged(skipSize,pageSize,this.$state);
                });
                // gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
                gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
            }

        }
        this.$scope.query = this.query;
        this.query();
        this.loadOrderStatusDropDown();
        this.loadDispatchOfficerDropDown();
        this.loadCustomerServiceOfficerDropDown();

        /*datetimepicker*/
        $('#openStartTime').click(function () {
            $('#shipStartTime').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#shipEndTime').datetimepicker('show');
        });
        $('#openPStartTime').click(function () {
            $('#placeOrderStartTime').datetimepicker('show');
        });
        $('#openPEndTime').click(function () {
            $('#placeOrderEndTime').datetimepicker('show');
        });


    }


    /**
     * 加载数据
     */
    loadData(queryTerms: IOrderManagentQueryTerms, skip: number, count: number): void {
        if (this.$scope.queryTerms.status === "" || this.$scope.queryTerms.status === null || this.$scope.queryTerms.status === undefined) { queryTerms.status = "1,2,8"; };
        this.orderService.list(this.orderBy.toString(), this.asc, queryTerms.orderId, queryTerms.clientName,
            queryTerms.route, queryTerms.shipStartTime, queryTerms.shipEndTime,
            queryTerms.status, queryTerms.placeOrderStartTime, queryTerms.placeOrderEndTime,
            queryTerms.dispatchOfficerId, queryTerms.shipAddress, queryTerms.deliverAddress,
            queryTerms.customerServiceOfficerId, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                 /**
                 * 设置列表高度
                 */
                
                this.$scope.heightA = ((result.data.data.length)*39)+96;
                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.skip = skip;

                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            })
    }
    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }
    /**
     * 订单管理导出
     */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>30000){
            this.$ngBootbox.alert('导出数据不可超过3万条');
            return
        }else{
            this.exportService.exportOrderList(this.orderBy,this.asc,this.queryTerms.orderId,this.queryTerms.clientName,this.queryTerms.route,this.queryTerms.shipStartTime,this.queryTerms.shipEndTime,this.queryTerms.status,this.queryTerms.placeOrderStartTime,this.queryTerms.placeOrderEndTime,this.queryTerms.dispatchOfficerId,this.queryTerms.shipAddress,this.queryTerms.deliverAddress,this.queryTerms.customerServiceOfficerId)
        }    
        
    }

    /**
     * 订单状态下拉
     */
    loadOrderStatusDropDown=()=> {
        this.$scope.statusList =[{
                "text": "派车中",
                "value": "1"
            },
                {
                    "text": "退回下单",
                    "value": "2"
                },
                
                {
                    "text": "订单终结",
                    "value": "8"
                }]
        
    }

    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown = () => {
        this.employeeService.getbytype("2", 0, -1).then(result => {
            this.$scope.dispatchOfficerList = result.data.data;
        });
    }

    /**
     * 填充客服专员下拉
     */
    loadCustomerServiceOfficerDropDown = ()=> {
        this.employeeService.getbytype("4", 0, -1).then(result => {
            this.$scope.customerOfficerList = result.data.data;
        })
    }

    /**
     * 排序事件触发
     */
    sortChange=(grid: uiGrid.IGridInstanceOf<orderList>, sortColumns: Array<uiGrid.IGridColumnOf<orderList>>)=> {
        if (sortColumns.length == 0) {
            this.query();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "orderId": {
                    this.orderBy = 1;
                } break;
                case "clientName": {
                    this.orderBy = 2;
                } break;
                case "startAddress": {
                    this.orderBy = 3;
                } break;
                case "viaAddress": {
                    this.orderBy = 4;
                } break;
                case "endAddress": {
                    this.orderBy = 5;
                } break;
                case "goodsName": {
                    this.orderBy = 6;
                } break;
                case "goodsNum": {
                    this.orderBy = 7;
                } break;
                case "carLength": {
                    this.orderBy = 8;
                } break;
                case "shipTime": {
                    this.orderBy = 9;
                } break;
                case "urgency": {
                    this.orderBy = 10;
                } break;
                case "orderStatus": {
                    this.orderBy = 11;
                } break;
                case "placeOrderTime": {
                    this.orderBy = 12;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.query();
        }


    }

    /**
     * 单选事件
     */
    selectChange = (row: uiGrid.IGridRowOf<orderList>)=> {
        row.isSelected ? this.id = row.entity.id : this.id = "";
    }

    /**
     * 查看详情
     */
    orderAddEdit = ()=> {
        if (!this.id) { this.$ngBootbox.alert("请选择一项"); return; }
        this.$state.go("app.order.orderManagentDetailController", { id: this.id, name: 'Edit' })
    }


    

}

angular.module("tms").controller("orderManagentController", ['$scope','orderService', 'valueService',  '$ngBootbox', 
  '$state', 'employeeService','routeService', '$http',"$location","exportService",OrderManagentController]);